package com.example.smartwaterapi.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.smartwaterapi.entity.MonthlyStats;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 月度统计Mapper接口
 */
@Mapper
public interface MonthlyStatsMapper extends BaseMapper<MonthlyStats> {
    
    /**
     * 获取用户指定年月的统计数据
     */
    @Select("SELECT * FROM monthly_stats WHERE user_id = #{userId} AND year = #{year} AND month = #{month}")
    MonthlyStats getUserMonthlyStats(@Param("userId") Long userId, @Param("year") Integer year, @Param("month") Integer month);
    
    /**
     * 获取用户指定年份的所有月度统计
     */
    @Select("SELECT * FROM monthly_stats WHERE user_id = #{userId} AND year = #{year} ORDER BY month")
    List<MonthlyStats> getUserYearlyStats(@Param("userId") Long userId, @Param("year") Integer year);
    
    /**
     * 获取用户最近N个月的统计数据
     */
    @Select("SELECT * FROM monthly_stats " +
            "WHERE user_id = #{userId} " +
            "ORDER BY year DESC, month DESC LIMIT #{limit}")
    List<MonthlyStats> getUserRecentMonthlyStats(@Param("userId") Long userId, @Param("limit") Integer limit);
    
    /**
     * 获取用户年度达标率趋势
     */
    @Select("SELECT year, month, achievement_rate FROM monthly_stats " +
            "WHERE user_id = #{userId} AND year = #{year} " +
            "ORDER BY month")
    List<Map<String, Object>> getUserYearlyAchievementTrend(@Param("userId") Long userId, @Param("year") Integer year);
    
    /**
     * 获取用户年度饮水量趋势
     */
    @Select("SELECT year, month, total_amount, avg_daily_amount FROM monthly_stats " +
            "WHERE user_id = #{userId} AND year = #{year} " +
            "ORDER BY month")
    List<Map<String, Object>> getUserYearlyWaterTrend(@Param("userId") Long userId, @Param("year") Integer year);
    
    /**
     * 获取用户年度健康评分趋势
     */
    @Select("SELECT year, month, overall_health_score, consistency_score FROM monthly_stats " +
            "WHERE user_id = #{userId} AND year = #{year} " +
            "ORDER BY month")
    List<Map<String, Object>> getUserYearlyHealthScoreTrend(@Param("userId") Long userId, @Param("year") Integer year);
} 